<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/users' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 添加角色区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button
            type="primary"
            @click="setRolesDialog"
          >添加角色</el-button>
        </el-col>
      </el-row>

      <!-- 角色权限表格 -->
      <el-table
        :data='rolelist'
        border
        stripe
      >
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item1) in scope.row.children"
              :key="item1.id"
              class='botbor'
            >
              <!-- 第一列 -->
              <el-col :span='5'>
                <el-tag>{{item1.authName}}</el-tag>
                <span class="el-icon-caret-right"></span>
              </el-col>
              <!-- 第二列 -->
              <el-col :span='19'>
                <el-row
                  v-for="(item2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span='5'>
                    <el-tag type="success">{{item2.authName}}</el-tag>
                  </el-col>
                  <el-col :span="19">
                    <el-tag
                      v-for="(item3) in item2.children"
                      :key="item3.id"
                      type="warning"
                      closable
                      @close="tagClose(scope.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

          </template>
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
        ></el-table-column>
        <el-table-column
          label="角色名称"
          prop="roleName"
        ></el-table-column>
        <el-table-column
          label="角色描述"
          prop="roleDesc"
        ></el-table-column>
        <!-- 操作按钮区域 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="changeRolesShow(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="deleteRolesShow(scope.row)"
            >删除</el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="setRolesRights(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 设置用户权限弹窗 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setRolesdialogVisible"
      width="50%"
      @close="setRolesDialogClose"
    >
      <el-tree
        :data="userRoleslist"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="rolesID"
        ref="rolesRef"
      ></el-tree>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="setRolesdialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addRoles"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加角色弹窗 -->
    <el-dialog
      title="添加角色"
      :visible.sync="addRolesdialogVisible"
      width="50%"
      @close="addRolesDialogClose"
    >
      <div class='inputBoxs'> <span>角色名称:</span>
        <el-input
          v-model='addRolesName'
          placeholder="请输入新角色名称"
        ></el-input>
      </div>
      <div class='inputBoxs'> <span>角色描述:</span>
        <el-input
          v-model='addRolesDesc'
          placeholder="请输入新角色描述"
        ></el-input>
      </div>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addRolesdialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="setRoles"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑角色弹窗 -->
    <el-dialog
      title="编辑角色"
      :visible.sync="changeRolesdialogVisible"
      width="50%"
      @close="changeRolesdialogVisibleClose"
    >
      <div class='inputBoxs'> <span>角色名称:</span>
        <el-input
          v-model='addRolesName'
          placeholder="请输入新角色名称"
        ></el-input>
      </div>
      <div class='inputBoxs'> <span>角色描述:</span>
        <el-input
          v-model='addRolesDesc'
          placeholder="请输入新角色描述"
        ></el-input>
      </div>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="changeRolesdialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="changeRoles"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除确认弹窗 -->
    <el-dialog
      title="删除角色"
      :visible.sync="deleteRolesdialogVisible"
      width="50%"
      @close="changeRolesdialogVisibleClose"
    >
      <span>确定要删除该角色吗？</span>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="deleteRolesdialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="deleteRoles"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      rolelist: [],
      setRolesdialogVisible: false,
      userRoleslist: [],
      defaultProps: {
        label: 'authName',
        children: 'children'
      },
      rolesID: [],
      // 设置角色分配ID
      roleID: '',
      addRolesdialogVisible: false,
      addRolesName: '',
      addRolesDesc: '',
      changeRolesdialogVisible: false, // 控制编辑角色弹窗的开关
      deleteRolesdialogVisible: false // 控制删除角色弹窗的开关
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    async getRoleList () {
      const { data: res } = await this.$http.get('roles')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.$message.success('获取成功')
      this.rolelist = res.data
      // console.log(this.rolelist)
    },
    // 删除用户权限事件
    async tagClose (roles, id) {
      const confirm = await this.$confirm('此操作将永久删除用户权限, 是否继续?', '警告！', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(confirm)
      if (confirm === !'confirm') return this.$message.error('已取消')
      const { data: res } = await this.$http.delete(`roles/${roles.id}/rights/${id}`)
      if (res.meta.status !== 200) return this.$message.error('请求失败')
      this.$message.success('删除成功')
    },
    // 设置用户权限事件
    async setRolesRights (role) {
      this.roleID = role.id
      const { data: res } = await this.$http.get('rights/tree')
      this.userRoleslist = res.data
      this.getLeafID(role, this.rolesID)
      // console.log(this.rolesID)
      this.setRolesdialogVisible = true
    },
    getLeafID (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => this.getLeafID(item, arr)
      )
    },
    setRolesDialogClose () {
      this.rolesID = []
    },
    async addRoles () {
      const keys = [
        ...this.$refs.rolesRef.getCheckedKeys(),
        ...this.$refs.rolesRef.getHalfCheckedKeys()
      ]
      // console.log(keys)
      const rolesStr = keys.join(',')
      // console.log(rolesStr)
      const { data: res } = await this.$http.post(`roles/${this.roleID}}/rights`, { rids: rolesStr })
      if (res.meta.status !== 200) return this.$message.error('分配失败')
      this.$message.success('分配成功')
      this.setRolesdialogVisible = false
      this.getRoleList()
    },
    // 添加角色事件弹窗弹出！
    setRolesDialog () {
      this.addRolesdialogVisible = true
    },
    // 点击确定按钮提交添加角色事件
    async setRoles () {
      console.log(this.addRolesName.trim())
      if (this.addRolesName.trim() === null) return this.$message.error('角色名称不能为空')
      const { data: res } = await this.$http.post('roles', { roleName: this.addRolesName, roleDesc: this.addRolesDesc })
      if (res.meta.status !== 200) return this.$message.error('提交失败')
      this.$message.success('提交成功')
      this.addRolesdialogVisible = false
      this.getRoleList()
    },
    // 关闭添加角色弹窗时清空数据
    addRolesDialogClose () {
      this.addRolesName = ''
      this.addRolesDesc = ''
    },
    // 控制编辑角色弹窗开关
    changeRolesShow (role) {
      // 从服务器请求角色数据
      this.addRolesName = role.roleName
      this.addRolesDesc = role.roleDesc
      this.roleID = role.id
      this.changeRolesdialogVisible = true
    },
    // 提交修改玩的角色信息
    async changeRoles () {
      const { data: res } = await this.$http.put(`roles/${this.roleID}`, { roleName: this.addRolesName, roleDesc: this.addRolesDesc })
      if (res.meta.status !== 200) return this.$message.error('修改失败')
      this.$message.success('修改成功')
      this.changeRolesdialogVisible = false
      this.getRoleList()
    },
    // 关闭编辑角色弹窗时清空数据
    changeRolesdialogVisibleClose () {
      this.addRolesName = ''
      this.addRolesDesc = ''
      this.roleID = ''
    },
    // 删除功能弹窗弹出
    deleteRolesShow (role) {
      this.roleID = role.id
      this.deleteRolesdialogVisible = true
    },
    // 删除角色
    async deleteRoles () {
      const { data: res } = await this.$http.delete(`roles/${this.roleID}`)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.deleteRolesdialogVisible = false
      this.getRoleList()
    }

  }
}
</script>

<style scoped>
.botbor {
  border-bottom: 1px solid #ccc;
  padding: 7px 0;
  margin-left: 30px;
}
.el-input {
  width: 80%;
  margin-left: 20px;
}
.inputBoxs {
  margin-top: 30px;
}
</style>
